// server update code

function AvatarServerInfo(AAvatar) {
  // gather info from avatar to be sent to server
  this.i_x = AAvatar.x;
  this.i_y = AAvatar.y;
  this.v_action = AAvatar.action;
  this.v_animation = AAvatar.animation;
  this.j_head = (AAvatar.head) ? AAvatar.head.eqty : 0;
  this.j_hair = (AAvatar.hairstyle) ? AAvatar.hairstyle.eqty : 0;
  this.j_chest = (AAvatar.chest) ? AAvatar.chest.eqty : 0;
  this.j_leg = (AAvatar.leg) ? AAvatar.leg.eqty : 0;
  this.j_feet = (AAvatar.feet) ? AAvatar.feet.eqty : 0;
  this.j_hands = (AAvatar.hands) ? AAvatar.hands.eqty : 0;
  this.r = (AAvatar.hairstyle) ? AAvatar.hairstyle.sprite.red : -1;
  this.g = (AAvatar.hairstyle) ? AAvatar.hairstyle.sprite.green : -1;
  this.b = (AAvatar.hairstyle) ? AAvatar.hairstyle.sprite.blue : -1;
  this.v_race = AAvatar.race;
  
  this.compare = function(AInfo) {
    // return true if this is different than AInfo
    // NOTE: this will probably be used only in websockets version
    return (
      (this.i_x != AInfo.i_x)||
      (this.i_y != AInfo.i_y)|| 
      (this.v_action != AInfo.v_action)|| 
      (this.v_animation != AInfo.v_animation)|| 
      (this.j_head != AInfo.j_head)|| 
      (this.j_hair != AInfo.j_hair)|| 
      (this.j_chest != AInfo.j_chest)|| 
      (this.j_leg != AInfo.j_leg)|| 
      (this.j_feet != AInfo.j_feet)|| 
      (this.j_hands != AInfo.j_hands)|| 
      (this.r != AInfo.r)|| 
      (this.g != AInfo.g)|| 
      (this.b != AInfo.b)|| 
      (this.v_race != AInfo.v_race)
      );
    
  }
}

function AvatarUpdate(AAvatar) {
  // looks if MY avatar changed, send changes to server for update
  this.me = AAvatar;
  this.old = new AvatarServerInfo(this.me);
  this.start = 0;
  
  this.update = function(AOnDoneCallback,AWrite,AChatMessage) {
    this.start = (new Date()).getTime();
    var current = new AvatarServerInfo(this.me);
    var change = current.compare(this.old);
    //debug(' change='+change);
    // parameters string
    var params = 
      'pu_user_csrf='+pu_user_csrf;
    // write my params only when AWrite is true, AWrite=false is only used for peeking (first time update)
    if (AWrite) 
      params +=
      '&i_x='+current.i_x+
      '&i_y='+current.i_y+
      '&v_action='+current.v_action+
      '&v_animation='+current.v_animation+
      '&j_head='+current.j_head+
      '&j_hair='+current.j_hair+
      '&i_red='+current.r+
      '&i_green='+current.g+
      '&i_blue='+current.b+
      '&j_chest='+current.j_chest+
      '&j_leg='+current.j_leg+
      '&j_feet='+current.j_feet+
      '&j_hands='+current.j_hands+
      '&v_race='+current.v_race+
      '&v_chat='+AChatMessage; 
    //debug('asta-save: '+params);
    // remember current position
    this.old = current;
    // ajax
    puXtrAjax('server/asta.php',params,AOnDoneCallback,null);
  }
  
  this.duration = function() {
    // return one roundtrip to server duration
    var ms = (new Date()).getTime() - this.start;
    return ms+'ms';
  }
}
